package com.formiaomiao.demain.service.impl;

import com.formiaomiao.demain.dao.*;
import com.formiaomiao.demain.model.*;
import com.formiaomiao.demain.service.FollowService;
import com.formiaomiao.demain.utils.ResultUtil;
import com.formiaomiao.demain.utils.sortClass;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(value = "followService")
public class FollowServiceImpl implements FollowService {

    @Autowired
    FollowDao followDao;
    @Autowired
    private DemainDao demainDao;
    @Autowired
    private ImgDao imgDao;
    @Autowired
    private LikeDao likeDao;
    @Autowired
    private CommentDao commentDao;
    @Autowired
    private FriendRemarksDao friendRemarksDao;

    @Override
    public Result<String> InsertFollow(FollowBean followBean) {
        if (followDao.InsertFollow(followBean) == 0) {
            return ResultUtil.error(400, "关注失败");
        } else {
            return ResultUtil.success("关注成功");
        }
    }

    @Transactional(value = "demain")
    @Override
    public Result<String> DeleteFollow(FollowBean followBean) {
        if (followDao.DeleteFollow(followBean) == 0) {
            return ResultUtil.error(400, "取消关注失败");
        } else {
            return ResultUtil.success("取消关注成功");
        }
    }

    @Override
    public Result<FollowBean> SelectFollow(Integer followfrom, int pageNum, int pageSize) {
        if (followDao.SelectFollow(followfrom).size() == 0) {
            return ResultUtil.error(400, "暂没有动态信息");
        } else if (followDao.SelectFollow(followfrom).size() > 0) {
            PageHelper.startPage(pageNum, pageSize);
            List<FollowBean> followBeanList = followDao.SelectFollow(followfrom);
            List<DemainBean> list = new ArrayList<>();
            Map<String,DemainBean> demainBeanMap = new HashMap<>();
            for (int i = 0; i < followBeanList.size(); i++) {
                list.addAll(demainDao.SelectDemain(followBeanList.get(i).getFollowto()));
                for (int j = 0; j < list.size(); j++) {
                    FriendRemarksBean friendRemarksBean = new FriendRemarksBean();
                    friendRemarksBean.setFriendfrom(followfrom);
                    friendRemarksBean.setFriendto(list.get(i).getIduser());
                    if (friendRemarksDao.SelectRemarks(friendRemarksBean) == null) {
                        list.get(i).setFriendremarks("");
                    } else {
                        list.get(i).setFriendremarks(friendRemarksDao.SelectRemarks(friendRemarksBean)
                                .getFriendremarks() == null ? "" : friendRemarksDao.SelectRemarks(friendRemarksBean)
                                .getFriendremarks());
                    }
                }
            }
            for (int s = 0; s < list.size(); s++) {
                list.get(s).setImgBean(imgDao.imgidlist(list.get(s).getDataid()));
                int likecount = 0;
                for (int j = 0; j < likeDao.selectLike(list.get(s).getDataid()).size(); j++) {
                    if (likeDao.selectLike(list.get(s).getDataid()).get(j).getLiketype() == 1) {
                        likecount += 1;
                    }
                }
                list.get(s).setLikecount(likecount);
                int msgcount = 0;
                for (int j = 0; j < commentDao.SelectComment(list.get(s).getDataid()).size(); j++) {
                    msgcount += 1;
                }
                list.get(s).setMsgcount(msgcount);
                if (likeDao.checkLike(list.get(s).getDataid(), followfrom) == null) {
                    list.get(s).setIslike(0);
                } else {
                    list.get(s).setIslike(likeDao.checkLike(list.get(s).getDataid(), followfrom).getLiketype());
                }
            }
            for (int i = 0; i < list.size(); i++) {
                demainBeanMap.put(list.get(i).getDataid(),list.get(i));
            }
            for (int i = 0; i < followBeanList.size(); i++) {
                List<DemainBean> lists = new ArrayList<>();
                lists.add(demainBeanMap.get(followBeanList.get(i).getDataid()));
                followBeanList.get(i).setDemainList(lists);
            }
            sortClass sort = new sortClass();
            Collections.sort(followBeanList, sort);
            ResultData result = new ResultData(pageNum, pageSize, followBeanList);
            return ResultUtil.success(result);
        } else {
            return ResultUtil.error(400, "获取失败");
        }
    }

    @Override
    public Result<FollowBean> SelecetUserFollow(Integer id, int pageNum, int pageSize) {
        if (followDao.SelectUserFollow(id).size() == 0) {
            return ResultUtil.error(400, "暂没有关注人");
        } else if (followDao.SelectUserFollow(id).size() > 0) {
            PageHelper.startPage(pageNum, pageSize);
            List<UserBean> followBeanList = followDao.SelectUserFollow(id);
            for (int i = 0; i < followBeanList.size(); i++) {
                FriendRemarksBean friendRemarksBean = new FriendRemarksBean();
                friendRemarksBean.setFriendfrom(id);
                friendRemarksBean.setFriendto(followBeanList.get(i).getId());
                if (friendRemarksDao.SelectRemarks(friendRemarksBean) == null) {
                    followBeanList.get(i).setFriendremarks("");
                } else {
                    followBeanList.get(i).setFriendremarks(friendRemarksDao.SelectRemarks(friendRemarksBean)
                            .getFriendremarks() == null ? "" : friendRemarksDao.SelectRemarks(friendRemarksBean)
                            .getFriendremarks());
                }
            }
            ResultData result = new ResultData(pageNum, pageSize, followBeanList);
            return ResultUtil.success(result);
        } else {
            return ResultUtil.error(400, "获取失败");
        }
    }

    @Override
    public Result<FollowBean> SelecetFansFollow(Integer id, int pageNum, int pageSize) {
        if (followDao.SelectFansFollow(id).size() == 0) {
            return ResultUtil.error(400, "暂没有粉丝");
        } else if (followDao.SelectFansFollow(id).size() > 0) {
            PageHelper.startPage(pageNum, pageSize);
            List<UserBean> followBeanList = followDao.SelectUserFollow(id);
            for (int i = 0; i < followBeanList.size(); i++) {
                FriendRemarksBean friendRemarksBean = new FriendRemarksBean();
                friendRemarksBean.setFriendfrom(id);
                friendRemarksBean.setFriendto(followBeanList.get(i).getId());
                if (friendRemarksDao.SelectRemarks(friendRemarksBean) == null) {
                    followBeanList.get(i).setFriendremarks("");
                } else {
                    followBeanList.get(i).setFriendremarks(friendRemarksDao.SelectRemarks(friendRemarksBean)
                            .getFriendremarks() == null ? "" : friendRemarksDao.SelectRemarks(friendRemarksBean)
                            .getFriendremarks());
                }
            }
            ResultData result = new ResultData(pageNum, pageSize, followBeanList);
            return ResultUtil.success(result);
        } else {
            return ResultUtil.error(400, "获取失败");
        }
    }

    /**
     * 查询是否关注
     *
     * @param followBean
     * @return
     */
    @Override
    public Result<String> CheckFollow(FollowBean followBean) {
        if (followDao.CheckFollow(followBean) != null) {
            return ResultUtil.success(true);
        } else {
            return ResultUtil.success(false);
        }
    }

    private List<DemainBean> removeSame(List<DemainBean> list) {
        HashSet<DemainBean> set = new HashSet<DemainBean>(list.size());
        List<DemainBean> result = new ArrayList<DemainBean>(list.size());
        for (DemainBean str : list) {
            if (set.add(str)) {
                result.add(str);
            }
        }
        list.clear();
        list.addAll(result);
        return list;
    }
}
